﻿Imports TodoMicroSolutionPackV1.bl.bo
Imports TodoMicroSolutionPackV1.facturacion.dal.dto
Imports TodoMicroSolutionPackV1.dal.dao
Imports TodoMicroSolutionPackV1.bl
Imports TodoMicroSolutionPackV1.productos.dal.dto
Imports TodoMicroSolutionPackV1.dal.dto

Namespace facturacion.bl.bo
    Public Class comprobanteItemBO
        Inherits genericNhiberateBusinessObject(Of Integer, comprobanteItem)

        Private _item As comprobanteItem

        Public Sub New(ByRef daoFactory As NhibernateDAOFactory, ByRef dto As comprobanteItem)
            MyBase.New(daoFactory, dto)
            _item = DirectCast(dto, comprobanteItem)
        End Sub

#Region "Rules"

        Public Overrides Function createRules() As System.Collections.Generic.List(Of Rule)
            Dim rules As List(Of Rule) = MyBase.createRules
            rules.Add(New SimpleRule("comprobante", "El item no tiene un comprobante asignado", Function() (_item Is Nothing) OrElse (Not _item.comprobante Is Nothing)))
            rules.Add(New SimpleRule("cantidad", "La cantidad debe ser un numero positivo", Function() (_item Is Nothing) OrElse (Not _item.cantidad >= 0)))
            rules.Add(New SimpleRule("producto", "No hay un producto seteado", Function() (_item Is Nothing) OrElse (Not _item.producto Is Nothing)))
            rules.Add(New SimpleRule("stock", "No hay un stock seteado", Function() (_item Is Nothing) OrElse (Not _item.stock Is Nothing)))
            rules.Add(New SimpleRule("iva", "No hay una tasa de iva seteada", Function() (_item Is Nothing) OrElse (Not _item.iva Is Nothing)))
            rules.Add(New SimpleRule("iva", "La constante referente a la tasa de iva no corresponde a una tasa de iva valida", AddressOf ivaConstantIsOK))
            rules.Add(New SimpleRule("precio_neto", "El precio neto debe ser un valor positivo", Function() (_item Is Nothing) OrElse (Not _item.precio_neto >= 0)))
            Return rules
        End Function

        Public Overridable Function ivaConstantIsOK() As Boolean
            Dim valid As Boolean
            Try
                valid = _item.iva.categoria = "facturacion" And _item.iva.tipo = "alicuota_iva"
            Catch ex As NullReferenceException
                valid = True
            End Try
            Return valid
        End Function
#End Region

        Public Overridable ReadOnly Property id As Integer
            Get
                Return _item.id
            End Get
        End Property
        Public Overridable Property comprobante As comprobante
            Get
                Return _item.comprobante
            End Get
            Set(ByVal value As comprobante)
                _item.comprobante = value
                NotifyChanged("comprobante")
            End Set
        End Property
        Public Overridable Property cantidad As Double
            Get
                Return _item.cantidad
            End Get
            Set(ByVal value As Double)
                _item.cantidad = value
                NotifyChanged("cantidad")
            End Set
        End Property
        Public Overridable Property producto As producto
            Get
                Return _item.producto
            End Get
            Set(ByVal value As producto)
                _item.producto = value
                NotifyChanged("producto")
            End Set
        End Property
        Public Overridable ReadOnly Property unidad_medida As constante
            Get
                Return _item.unidad_medida
            End Get
        End Property
        Public Overridable ReadOnly Property unidad_medida_codigo As Integer
            Get
                Return _item.unidad_medida_codigo
            End Get
        End Property
        Public Overridable ReadOnly Property unidad_medida_valor As String
            Get
                Return _item.unidad_medida_valor
            End Get
        End Property
        Public Overridable ReadOnly Property codigo As String
            Get
                Return _item.codigo
            End Get
        End Property
        Public Overridable Property descripcion As String
            Get
                Return _item.descripcion
            End Get
            Set(ByVal value As String)
                _item.descripcion = value
                NotifyChanged("descripcion")
            End Set
        End Property
        Public Overridable Property stock As stock
            Get
                Return _item.stock
            End Get
            Set(ByVal value As stock)
                _item.stock = value
                NotifyChanged("stock")
            End Set
        End Property
        Public Overridable ReadOnly Property numero_despacho As String
            Get
                Return _item.numero_despacho
            End Get
        End Property
        Public Overridable Property iva As constante
            Get
                Return _item.iva
            End Get
            Set(ByVal value As constante)
                _item.iva = value
                NotifyChanged("iva")
            End Set
        End Property
        Public Overridable Property precio_neto As Double
            Get
                Return _item.precio_neto
            End Get
            Set(ByVal value As Double)
                _item.precio_neto = value
                NotifyChanged("precio_neto")
            End Set
        End Property
        Public Overridable ReadOnly Property precio_bruto As Double
            Get
                Return _item.precio_bruto
            End Get
        End Property

    End Class
End Namespace

